//
//  ViewController.m
//  LouMusicDemo
//
//  Created by 漫步人生路 on 15/7/1.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "ViewController.h"
#import "BlurImageView.h"
#import "SongListTableViewCell.h"
#import "LORequestManger.h"
#import "SongListModel.h"
#import "PlayViewController.h"
#import "LanOuMusic.h"
#define kSongList @"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&format=json&offset=0&size=50&from=ios&fields=title,song_id,author,resource_type,havehigh,is_new,has_mv_mobile,album_title,ting_uid,album_id,charge,all_rate&version=5.2.1&from=ios&channel=appstore"
@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *songsArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    BlurImageView *backImage = [[BlurImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:backImage];
    self.songListVC = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.songListVC.tableHeaderView.backgroundColor = [UIColor clearColor];
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)];
    headerView.backgroundColor = [UIColor clearColor];
    self.songListVC.backgroundColor = [UIColor clearColor];
    self.songListVC.tableHeaderView = headerView;
    self.songListVC.delegate = self;
    self.songListVC.dataSource = self;
    self.songListVC.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.songListVC];
    [self loadDataWithSongs];
}

- (NSMutableArray *)songsArray
{
    if (!_songsArray) {
        _songsArray = [NSMutableArray array];
    }
    return _songsArray;
}
- (void)loadDataWithSongs
{
    [LORequestManger GET:kSongList success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *songList in dic[@"song_list"]) {
            SongListModel *songListModel = [SongListModel songsModelWithDictionary:songList];
            [self.songsArray addObject:songListModel];
        }
        [self.songListVC reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songsArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.songListVC registerClass:[SongListTableViewCell class] forCellReuseIdentifier:@"songList"];
    SongListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songList" forIndexPath:indexPath];
    SongListModel *song = [[SongListModel alloc]init];
    song = [self.songsArray objectAtIndex:indexPath.row];
    cell.songName.text = song.album_title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayViewController *playVC = [[PlayViewController alloc]init];
    SongListModel *m = [[SongListModel alloc]init];
    for (m in self.songsArray) {
        NSString *songID = m.song_id;
        [[LanOuMusic shareMusic].songIDArray addObject:songID];
    }
    playVC.index = indexPath.row;
    SongListModel *songID = [[SongListModel alloc]init];
    songID = [self.songsArray objectAtIndex:indexPath.row];
    playVC.songID = songID.song_id;
    [self presentViewController:playVC animated:YES completion:nil];
//    [self.navigationController pushViewController:playVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
